<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="楼&nbsp;&nbsp;栋&nbsp;&nbsp;号" prop="towerNumber">
              <el-input v-model="queryParams.towerNumber" placeholder="请输入楼栋号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="房&nbsp;&nbsp;间&nbsp;&nbsp;号" prop="roomNumber">
              <el-input v-model="queryParams.roomNumber" placeholder="请输入房间号" clearable @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item label="房间环境" prop="roomEnvironment">
              <el-input v-model="queryParams.roomEnvironment" placeholder="请输入房间环境" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="房间状态" prop="roomState">
              <el-select v-model="queryParams.roomState" placeholder="请选择房间状态" clearable>
                <el-option v-for="dict in parkbuilingroom_state" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="缴租频次" prop="frentPaymentFrequency">
              <el-select v-model="queryParams.frentPaymentFrequency" placeholder="请选择缴租频次">
                <el-option v-for="dict in park_frent_payment_frequency" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="到期提醒">
              <el-select v-model="queryParams.expirationReminder" placeholder="请选择到期提醒">
                <el-option v-for="dict in park_expiration_reminder" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <div style="display: flex; justify-content: flex-end; align-items: center">
          <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
          <el-button plain icon="Plus" @click="handleAdd"> 新增 </el-button>
          <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
        </div>
      </template>

      <el-table v-loading="loading" :data="flatbasicInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键标识" align="center" prop="id" />
        <el-table-column label="序号" align="center">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="楼栋号" align="center" prop="towerNumber" />
        <el-table-column label="楼层" align="center" prop="floor" />
        <el-table-column label="房间号" align="center" prop="roomNumber" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="房间环境" align="center" prop="roomEnvironment">
          <template #default="scope">
            <dict-tag :options="parkr_building_room_environment" :value="scope.row.roomEnvironment" />
          </template>
        </el-table-column>
        <el-table-column label="房间状态" align="center" prop="roomState">
          <template #default="scope">
            <dict-tag :options="parkbuilingroom_state" :value="scope.row.roomState" />
          </template>
        </el-table-column>
        <el-table-column label="承租人" align="center" prop="tenantry" />
        <el-table-column label="租金(元/月)" align="center" prop="rents" />
        <el-table-column label="开始时间" width="120" align="center" prop="startTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" width="120" align="center" prop="endTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="缴租频次" align="center" prop="frentPaymentFrequency">
          <template #default="scope">
            <dict-tag :options="park_frent_payment_frequency" :value="scope.row.frentPaymentFrequency" />
          </template>
        </el-table-column>
        <el-table-column label="到期提醒" align="center" prop="expirationReminder">
          <template #default="scope">
            <dict-tag :options="park_expiration_reminder" :value="scope.row.expirationReminder" />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="120" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link type="primary" @click="handleView(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <FlatFormDialog ref="flatFormDialogRef" @refresh="getList" />
  </div>
</template>

<script setup name="FlatbasicInfo" lang="ts">
import FlatFormDialog from '@/components/FlatFormDialog/index.vue';
import { listFlatbasicInfo, delFlatbasicInfo } from '@/api/zichanguanli/flatbasicInfo';
import { FlatbasicInfoVO, FlatbasicInfoQuery, FlatbasicInfoForm } from '@/api/zichanguanli/flatbasicInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { parkbuilingroom_state, park_frent_payment_frequency, parkr_building_room_environment, park_expiration_reminder } = toRefs<any>(
  proxy?.useDict(
    'parkbuilingroom_state',
    'park_surrender_tenancy_status',
    'park_frent_payment_frequency',
    'parkr_building_room_environment',
    'park_expiration_reminder'
  )
);
import { FlatrentalInfoForm } from '@/api/zichanguanli/flatrentalInfo/types';
const flatbasicInfoList = ref<FlatbasicInfoVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const flatbasicInfoFormRef = ref<ElFormInstance>();

const flatFormDialogRef = ref(null);
const flatFormDialogOpen = (type: string, id?: string | number) => {
  flatFormDialogRef.value.open(type, id);
};

const initFormData: FlatbasicInfoForm = {
  id: undefined,
  towerNumber: undefined,
  name: undefined,
  roomNumber: undefined,
  floorArea: undefined,
  roomEnvironment: undefined,
  roomState: undefined,
  apartmentValuation: undefined,
  remark: undefined,
  attachments: undefined
};
const data = reactive<PageData<FlatbasicInfoForm, FlatbasicInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    expirationReminder: undefined,
    towerNumber: undefined,
    name: undefined,
    roomEnvironment: undefined,
    roomNumber: undefined,
    roomState: undefined,
    tenantry: undefined
  },
  rules: {
    id: [{ required: true, message: '主键标识不能为空', trigger: 'blur' }],
    towerNumber: [{ required: true, message: '楼栋号不能为空', trigger: 'blur' }],
    name: [{ required: true, message: '公寓名称不能为空', trigger: 'blur' }],
    floor: [{ required: true, message: '所在楼层不能为空', trigger: 'blur' }],
    roomNumber: [{ required: true, message: '房间号不能为空', trigger: 'blur' }],
    floorArea: [{ required: true, message: '建筑面积(㎡)不能为空', trigger: 'blur' }],
    roomEnvironment: [{ required: true, message: '房间环境不能为空', trigger: 'blur' }],
    roomState: [{ required: true, message: '房间状态不能为空', trigger: 'change' }],
    apartmentValuation: [{ required: true, message: '公寓估值不能为空', trigger: 'change' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
    attachments: [{ required: true, message: '公寓附件不能为空', trigger: 'blur' }]
  }
});

const rentalFormData: FlatrentalInfoForm = {
  id: undefined,
  apartmentBasicInfoId: undefined,
  name: undefined,
  frentPaymentFrequency: undefined,
  rents: undefined,
  contactInformation: undefined,
  startTime: undefined,
  endTime: undefined,
  leaseTerm: undefined,
  propertyCostStandard: undefined,
  remark: undefined,
  attachments: undefined,
  surrenderTenancyStatus: undefined,
  rentOutTime: undefined
};
const rentalFormDataFef = ref<FlatrentalInfoForm>();
rentalFormDataFef.value = { ...rentalFormData };
const { queryParams, form, rules } = toRefs(data);

/** 查询公寓基础信息列表 */
const getList = async () => {
  loading.value = true;
  console.log(123);
  const res = await listFlatbasicInfo(queryParams.value);
  flatbasicInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  flatbasicInfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: FlatbasicInfoVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  flatFormDialogOpen('create');
};

/** 修改按钮操作 */
const handleUpdate = (row?: FlatbasicInfoVO) => {
  const _id = row?.id || ids.value[0];
  flatFormDialogOpen('edit', _id);
};

/** 删除按钮操作 */
const handleDelete = async (row?: FlatbasicInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delFlatbasicInfo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

const handleView = (row?: FlatbasicInfoVO) => {
  const _id = row?.id || ids.value[0];
  flatFormDialogOpen('view', _id);
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('zichanguanli/flatbasicInfo/export', { ...queryParams.value }, `flatbasicInfo_${new Date().getTime()}.xlsx`);
};

onMounted(() => {
  getList();
});
</script>
